<template>
  <div class="marketing-model">
    <div v-show="currentRouter==1||currentRouter=='detail'">
      <div class="marketing-list" v-if="!isDetailShow">
        <div class="search-box">
          <div class="labelName lf">商品名称：</div>
          <el-input class="elInput lf" v-model="goods_name"></el-input>
          <div class="labelName lf">状态：</div>
          <el-select class="elInput lf" v-model="status" placeholder="请选择">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <div class="labelName lf">揭晓时间：</div>
          <el-date-picker @keyup.enter.native="searchData()" value-format="yyyy-MM-dd HH:mm:ss" class="elDate lf elTimepicker" v-model="start_time" type="date" placeholder="选择日期"></el-date-picker>
          <div class="labelName lf">至：</div>
          <el-date-picker @keyup.enter.native="searchData()" value-format="yyyy-MM-dd 23:59:59" class="elDate lf elTimepicker" v-model="end_time" type="date" placeholder="选择日期"></el-date-picker>
          <el-button class="lf" type="primary" @click="searchData()">查询</el-button>
        </div>
        <div class="table-box">
          <el-table :data="marketingList" @selection-change="clickRow" @sort-change="onSortChange">
            <el-table-column label="商品" width="250px;">
              <template slot-scope="scope">
                <div class="goodsDetail clearfix">
                  <div class="priviewImg lf">
                    <img :src="scope.row.goods_image" alt="">
                  </div>
                  <div class="goodsMsg lf">
                    {{scope.row.goods_name}}
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="成本/零售价">
              <template slot-scope="scope">
                <div class="control">
                  <div class="price">￥{{scope.row.sku_supply_price}}</div>
                  <div class="price">￥{{scope.row.sku_sell_price}}</div>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="开团数" prop="create_groups" sortable="custom"></el-table-column>
            <el-table-column label="成团数" prop="success_groups" sortable="custom"></el-table-column>
            <el-table-column label="新用户人数" prop="new_users" sortable="custom"></el-table-column>
            <el-table-column label="揭晓时间">
              <template slot-scope="scope">
                <div class="control">{{scope.row.end_time}}</div>
              </template>
            </el-table-column>
            <el-table-column label="状态">
              <template slot-scope="scope">
                <div class="control">{{scope.row.status_desc}}</div>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="140px">
              <template slot-scope="scope">
                <div class="control">
                  <a @click="openDetail(scope.row)">详情</a>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage"
            :page-sizes="pageSizeArray"
            :page-size="10"
            layout="sizes, prev, pager, next"
            :total="total_count">
          </el-pagination>
        </div>
      </div>
      <!-- 拼团活动详情 -->
      <div class="marketing-list" v-show="isDetailShow">

        <el-breadcrumb class="back-btn" separator-class="el-icon-arrow-right">
          <el-button  @click='appBack' type="primary"> < 返回 </el-button>
        </el-breadcrumb>

        <div class="search-box high">
          <el-row>
            <el-col :span="24">
              <div class="labelName lf">团长名称：</div>
              <el-input class="elInput lf" v-model="creator_name"></el-input>
              <div class="labelName lf">拼团状态：</div>
              <el-select class="elInput lf" v-model="status_detail" placeholder="请选择">
                <el-option
                  v-for="item in statusOptionsDetail"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <div class="labelName lf">发放状态：</div>
              <el-select class="elInput lf" v-model="award_status_detail" placeholder="请选择">
                <el-option
                  v-for="item in award_status_OptionsDetail"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <div class="labelName lf">开团时间：</div>
              <el-date-picker @keyup.enter.native="searchDataDetail()" value-format="yyyy-MM-dd HH:mm:ss" class="elDate lf elTimepicker" v-model="start_time_detail" type="date" placeholder="选择日期"></el-date-picker>
              <div class="labelName lf">至：</div>
              <el-date-picker @keyup.enter.native="searchDataDetail()" value-format="yyyy-MM-dd 23:59:59" class="elDate lf elTimepicker" v-model="end_time_detail" type="date" placeholder="选择日期"></el-date-picker>
              <el-button class="lf" type="primary" @click="searchDataDetail()">查询</el-button>
            </el-col>
          </el-row>

          <el-row style="margin-top: 16px;">
            <el-col :span="24">
              <div class="labelName lf">是否成团：</div>
              <el-select class="elInput lf" v-model="is_success" placeholder="请选择">
                <el-option
                  v-for="item in is_success_option"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-col>
          </el-row>
        </div>

        <div class="table-box">
          <el-table :data="detailList" @selection-change="clickRow">

            <el-table-column label="团ID" prop="group_id"></el-table-column>

            <el-table-column label="团长头像" width="250px;">
              <template slot-scope="scope">
                <div class="userAvatar clearfix">
                  <div class="">
                    <img :src="scope.row.creator_avatar" alt="">
                  </div>
                </div>
              </template>
            </el-table-column>

            <el-table-column label="团长昵称" prop="creator_name"></el-table-column>
            <el-table-column label="团人数" prop="join_numbers"></el-table-column>
            <el-table-column label="是否成团" prop="is_success_desc"></el-table-column>

            <el-table-column label="开团时间">
              <template slot-scope="scope">
                <div class="control">{{scope.row.create_time}}</div>
              </template>
            </el-table-column>
            <el-table-column label="成团时间">
              <template slot-scope="scope">
                <div class="control">{{scope.row.success_time}}</div>
              </template>
            </el-table-column>
            <el-table-column label="拼团状态" prop="status_desc"></el-table-column>
            <el-table-column label="发放状态" prop="award_status_desc"></el-table-column>


            <el-table-column label="操作" width="140px">
              <template slot-scope="scope">
                <div class="control">
                  <div v-if="scope.row.operate === '/'">{{scope.row.operate}}</div>
                  <a @click="doOperate(scope.row)" v-else>{{scope.row.operate}}</a>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="currentPage_detail"
            :page-sizes="[10,20,50]"
            :page-size="10"
            layout="sizes, prev, pager, next"
            :total="total_count_detail">
          </el-pagination>
        </div>
      </div>
    </div>

    <xiaoxiaole :type="currentRouter"></xiaoxiaole>
  </div>
</template>
<script>
  import xiaoxiaole from "@/page/xiaoxiaole";
  export default {
    name: "marketing",
    components: {
      xiaoxiaole: xiaoxiaole
    },
    watch: {
      $route(to, from) {
        this.currentRouter = to.params.type;
        console.log(this.currentRouter);
        if( this.currentRouter === "1" ){
          this.isDetailShow = false;
          this.currentPage_detail = 1;
          this.per_page = 10;
          this.getMarketingList();
        }
      }
    },
    methods: {
      //回退按钮
      appBack() {
        window.history.back();
      },

      //排序
      onSortChange(data) {
          console.log(data);
        this.sc_name = data.prop;
        //判断升序降序
        if (data.order === "ascending") {
          this.sc = 'asc'
        } else {
          this.sc = 'desc'
        }
        this.getMarketingList();
      },
      //切换页面
      handleCurrentChange(page) {
        this.currentPage = page;
        this.getMarketingList();
      },

      //分页 每页条数切换
      handleSizeChange(data) {
        if( this.isDetailShow === true ){
          this.per_page_detail = data;
          this.getDetail();
        }
        else {
          this.per_page = data;
          this.getMarketingList();
        }
      },

      //点击查询
      searchData() {
        this.currentPage = 1;
        this.getMarketingList();
      },
      //点击选择行数据
      clickRow(row) {
        this.userArr = row;
      },
      //获取拼团活动列表
      getMarketingList() {
        let formData = new FormData();
        formData.append("page", this.currentPage);
        formData.append("per_page", this.per_page);

        this.goods_id ? formData.append("goods_id", this.goods_id) : "" ;
        this.goods_name ? formData.append("goods_name", this.goods_name) : "" ;
        this.start_time ? formData.append("start_time", this.start_time) : "" ;
        this.end_time ? formData.append("end_time", this.end_time) : "" ;
        this.sc_name ? formData.append("sc_name", this.sc_name) : "" ;
        this.sc ? formData.append("sc", this.sc) : "" ;
        this.status !== "" ? formData.append("status", this.status) : "" ;

        this.$http(
          "/wgl_admin/index.php?app=spell&act=index",
          "post",
          {},
          {},
          formData,
          success => {
            this.marketingList = success.data.list;
            this.total_count = success.data.total_count;
          },
          error => {
            this.$message.error(error.state.msg);
          }
        );
      },

      //获取拼团详情
      getDetail() {
        let formData = new FormData();
        formData.append("page", this.currentPage_detail);
        formData.append("per_page", this.per_page_detail);

        this.ware_id ? formData.append("ware_id", this.ware_id) : "" ;
        this.status_detail !== "" ? formData.append("status", this.status_detail) : -1 ;
        this.award_status_detail !== "" ? formData.append("award_status", this.award_status_detail) : -1 ;
        this.is_success !== "" ? formData.append("is_success", this.is_success) : -1 ;
        this.start_time_detail ? formData.append("start_time", this.start_time_detail) : "" ;
        this.end_time_detail ? formData.append("end_time", this.end_time_detail) : "" ;
        this.creator_name ? formData.append("creator_name", this.creator_name) : "" ;

        this.$http(
          "/wgl_admin/index.php?app=spell&act=detail",
          "post",
          {},
          {},
          formData,
          success => {
            this.detailList = success.data.list;
            this.total_count_detail = success.data.total_count;
          },
          error => {
            this.$message.error(error.state.msg);
          }
        );
      },
      //操作按钮
      doOperate(data) {
        let formData = new FormData();
        formData.append("spell_group_id", data.group_id);

        this.$http(
          "/wgl_admin/index.php?app=spell&act=awards",
          "post",
          {},
          {},
          formData,
          success => {
            this.$message.success(success.state.msg);
          },
          error => {
            this.$message.error(error.state.msg);
          }
        );
      },

      openDetail(data) {
        this.clearDetail();
        this.$router.push({
          name: "marketing",
          params: {
              type: "detail"
          }
        });
        console.log(data);
        this.isDetailShow = !this.isDetailShow;
        this.ware_id = data.ware_id;
        this.getDetail();
      },

      //点击查询
      searchDataDetail() {
        this.currentPage_detail = 1;
        this.getDetail();
      },

      clearDetail() {
        this.detailList = [];
      }

    },
    created() {
      this.getMarketingList();
    },
    mounted() {
      this.currentRouter = this.$route.params.type;

      if ( this.currentRouter === "1" ){
        this.isDetailShow = false;
        this.currentPage_detail = 1;
      }
      if ( this.currentRouter === "detail" ){
        this.isDetailShow = true;
        this.currentPage = 1;
        this.$router.push({
          name: "marketing",
          params: {
            type: "1"
          }
        });
      }
    },
    data() {
      return {
        currentPage: 1, //当前的页面
        per_page: 10,
        pageSizeArray: PageSizeArray,
        total_count: 0,
        total_count_detail: 0,

        goods_id: "",
        goods_name: "",
        start_time: "",
        end_time: "",
        sc_name: "", //排序的字段
        sc: "", //升序或者是降序

        isDetailShow: false,
        currentPage_detail: 1,
        per_page_detail: 10,
        ware_id: "",
        creator_name: "",

        status_detail: -1,
        award_status_detail: -1,
        start_time_detail: "",
        end_time_detail: "",
        detailList: [],
        spell_group_id: "",
        currentRouter: "",

        userArr: [], //选中的用户列表
        marketingList: [],

        is_success: -1,
        is_success_option: [
          {
            value: -1,
            label: "全部"
          },
          {
            value: 0,
            label: "否"
          },
          {
            value: 1,
            label: "是"
          }
        ],

        status: "",
        statusOptions: [
          {
            value: -1,
            label: "全部"
          },
          {
            value: 1,
            label: "待揭晓"
          },
          {
            value: 2,
            label: "已发放"
          },
          {
            value: 3,
            label: "不发放"
          }
        ],
        statusOptionsDetail: [
          {
            value: -1,
            label: "全部"
          },
          {
            value: 0,
            label: "未成团"
          },
          {
            value: 1,
            label: "待揭晓"
          },
          {
            value: 2,
            label: "未中奖"
          },
          {
            value: 3,
            label: "已中奖"
          },
          {
            value: 4,
            label: "拼团失败"
          }
        ],
        award_status_OptionsDetail: [
          {
            value: -1,
            label: "全部"
          },
          {
            value: 0,
            label: "未发放"
          },
          {
            value: 1,
            label: "已发放"
          }
        ]
      };
    }
  };
</script>
<style lang="scss" scoped>
  .marketing-model {
    padding-left: 118px;
    .marketing-list {
      padding: 30px 56px 30px 50px;

      .back-btn {
        padding: 0 0 30px 0;
      }

      .search-box {
        border: 1px solid #e6e8f4;
        max-width: 1453px;
        min-width: 600px;
        width: 100%;
        height: 90px;
        padding: 25px 30px;
        &.high {
          height: 140px;
        }
        .elInput {
          display: block;
          width: 150px;
          font-size: 12px;
          color: #333;
          margin-right: 20px;
        }
        .labelName {
          height: 40px;
          line-height: 40px;
          color: #333;
          font-size: 14px;
        }
        .elTimepicker {
          display: block;
          width: 180px;
          font-size: 12px;
          color: #333;
          margin-right: 10px;
        }
      }
      .table-box {
        // max-width: 1466px;
        width: 100%;
        margin-top: 48px;
        .control {
          > a {
            color: #3f87f5;
            font-size: 12px;
            cursor: pointer;
          }
          .price {
            font-size: 12px;
            color: #e17474;
          }
        }

        .userAvatar {
          width: 80px;
          height: 80px;
          margin: 0 auto;
          img {
            width: 100%;
            height: 100%;
          }
        }

        .goodsDetail {
          .priviewImg {
            margin-right: 10px;
            > img {
              display: block;
              width: 80px;
              height: 80px;
            }
          }
          .goodsMsg {
            color: #333;
            font-size: 12px;
            max-width: 120px;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 4;
            overflow: hidden;
          }
        }
      }
    }
  }
  .pagination {
    width: 1466px;
    text-align: right;
    margin-top: 46px;
  }
</style>
